package com.qeeyou.apps.android.craftsman.activity.home;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import com.qeeyou.apps.android.craftsman.R;
import com.qeeyou.apps.android.craftsman.adapter.CommentAdapter;
import com.qeeyou.apps.android.craftsman.po.CommentInfo;
import com.qeeyou.apps.android.craftsman.po.MasterInfo;
import com.qeeyou.apps.android.craftsman.service.AppService;
import com.qeeyou.apps.android.craftsman.utils.ImageLoader;
import com.qeeyou.apps.android.craftsman.view.CustomLoadingDialog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MasterDetailActivity extends Activity {

    private ImageView btnBack;                    //返回按钮

    private ImageView imgvAvatar;
    private ImageView imgvGender;
    private TextView txtvServiceNum;
    private TextView txtvAge;
    private TextView txtvUsername;
    private TextView txtvScore;
    private RatingBar ratingBar;
    private TextView txtvAddress;
    private TextView txtvComments;

    private ListView lstvComment;                 //评论列表
    private LinearLayout btnCall;                 //叫他帮忙
    private List<CommentInfo> commentList;        //评论列表
    private CommentAdapter mAdapter;

    private CustomLoadingDialog mLoadingDialog;
    private MasterInfo masterInfo;
    private String pageNo;
    private String pageSize;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_master_detail);

        Intent intent = getIntent();
        if(intent != null && intent.getSerializableExtra("masterInfo") != null) {
            MasterDetailActivity.this.masterInfo = (MasterInfo) intent.getSerializableExtra("masterInfo");
        }

        setView();

        setEvent();
    }

    private void setView() {
        lstvComment = (ListView) this.findViewById(R.id.lstvComment4MasterDetail);

        View headerView = LayoutInflater.from(MasterDetailActivity.this).inflate(R.layout.view_master_detail_header, null);
        btnBack = (ImageView) headerView.findViewById(R.id.btnBack4MasterDetail);
        imgvAvatar = (ImageView) headerView.findViewById(R.id.imgvAvatar4MasterDetail);
        imgvGender = (ImageView) headerView.findViewById(R.id.imgvGender4MasterDetail);
        txtvServiceNum =(TextView)  headerView.findViewById(R.id.txtvServiceNum4MasterDetail);
        txtvAge =(TextView)  headerView.findViewById(R.id.txtvAge4MasterDetail);
        txtvUsername = (TextView) headerView.findViewById(R.id.txtvName4MasterDetail);
        ratingBar = (RatingBar) headerView.findViewById(R.id.ratingBar4MasterDetail);
        txtvScore = (TextView) headerView.findViewById(R.id.txtvScore4MasterDetail);
        txtvAddress = (TextView) headerView.findViewById(R.id.txtvAddress4MasterDetail);
        txtvComments = (TextView) headerView.findViewById(R.id.txtvComments4MasterDetail);

        lstvComment.addHeaderView(headerView, null, false);

        View footerView = LayoutInflater.from(MasterDetailActivity.this).inflate(R.layout.view_master_detail_footer, null, false);
        btnCall = (LinearLayout)footerView.findViewById(R.id.btnCall4MasterDetail);
        lstvComment.addFooterView(footerView, null, false);
    }

    private void setEvent() {

        btnBack.setOnClickListener(onBackClickListener);

        mAdapter = new CommentAdapter(MasterDetailActivity.this, commentList);
        lstvComment.setAdapter(mAdapter);
        lstvComment.setOnItemClickListener(onItemClickListener);

        btnCall.setOnClickListener(onCallClickListener);

        laodData();

    }

    //返回按钮
    private View.OnClickListener onBackClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            MasterDetailActivity.this.finish();
        }
    };

    //叫他帮忙
    private View.OnClickListener onCallClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //TODO -- 查看他的排班想信息
            Intent intent = new Intent(MasterDetailActivity.this, SchedulingActivity.class);
            MasterDetailActivity.this.startActivity(intent);
        }
    };

    private AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//            Toast.makeText(MasterDetailActivity.this, "评论Item点击事件", Toast.LENGTH_SHORT).show();
        }
    };


    private void laodData() {
        if(masterInfo != null && masterInfo.getMasterid() != null) {
            //显示师傅信息
            showMasterInfo(masterInfo);
            //获取评论列表
            pageNo = "0";
            pageSize = "20";
            String params[] = {masterInfo.getMasterid(), pageNo, pageSize};
            new GetMasterCommentList().execute(params);
        }
    }

    private void getCommentList() {
        if(commentList == null) {
            commentList = new ArrayList<CommentInfo>();
        }
        commentList.clear();

        CommentInfo commentInfo1 = new CommentInfo();
        commentInfo1.setOrderid("1");
        commentInfo1.setName("张三");
        commentInfo1.setUserAvatar(masterInfo.getAvatar());
        commentInfo1.setServicetime("2018-02-03 14:00:00");
        commentInfo1.setCommentmemo("这个师傅技术好，安装效率高，橱柜大气漂亮...");
        commentInfo1.setCommentimg("");
        commentInfo1.setSerquality("4.5");
        commentList.add(commentInfo1);

        CommentInfo commentInfo2 = new CommentInfo();
        commentInfo2.setOrderid("2");
        commentInfo2.setName("李四");
        commentInfo2.setUserAvatar("");
        commentInfo2.setServicetime("2018-02-03 14:00:00");
        commentInfo2.setCommentmemo("这个师傅技术好，安装效率高，橱柜大气漂亮...");
        commentInfo2.setCommentimg("");
        commentInfo2.setSerquality("3");
        commentList.add(commentInfo2);

        CommentInfo commentInfo3 = new CommentInfo();
        commentInfo3.setOrderid("1.5");
        commentInfo3.setName("王五");
        commentInfo3.setUserAvatar("");
        commentInfo3.setServicetime("2018-02-03 14:00:00");
        commentInfo3.setCommentmemo("这个师傅技术好，安装效率高，橱柜大气漂亮...");
        commentInfo3.setCommentimg("");
        commentInfo3.setSerquality("4");
        commentList.add(commentInfo3);

        mAdapter.setItems(commentList);
    }

    class GetMasterCommentList extends AsyncTask<String, Void, List<CommentInfo>> {
        public GetMasterCommentList() {
            super();
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            if(mLoadingDialog == null) {
                mLoadingDialog = new CustomLoadingDialog(MasterDetailActivity.this);
            }
            mLoadingDialog.setMessage("正在获取师傅详情...");
            mLoadingDialog.show();
        }

        @Override
        protected List<CommentInfo> doInBackground(String... params) {
            String masterId = params[0];
            String pageIndex = params[1];
            String pageSize = params[2];
            Map<String, String> paramMap = new HashMap<String, String>();
            paramMap.put("masterId", masterId);
            paramMap.put("pageIndex", pageIndex);
            paramMap.put("pageSize", pageSize);
            List<CommentInfo> info = AppService.getInstance().getMasterCommentsInfo(paramMap);
            return info;
        }

        @Override
        protected void onPostExecute(List<CommentInfo> commentList) {
            super.onPostExecute(commentList);
            if(mLoadingDialog != null) {
                mLoadingDialog.dismiss();
                mLoadingDialog = null;
            }
            //TODO -- 数据
            if(commentList != null && !commentList.isEmpty()) {
                mAdapter.setItems(commentList);
                txtvComments.setText("他的评价(" + commentList.size() + ")");
            } else {
                mAdapter.setItems(null);
                txtvComments.setText("他的评价(" + 0 + ")");
            }
        }

    }

    //显示师傅信息
    private void showMasterInfo(MasterInfo masterInfo) {
            //TODO -- 师傅详情页面
            String sex = masterInfo.getSex();
            if (sex == null || !sex.equalsIgnoreCase("1")) {
                imgvGender.setImageResource(R.drawable.ic_gender_female_master_detail);
            } else {
                imgvGender.setImageResource(R.drawable.ic_gender_male_master_detail);
            }


            String name = masterInfo.getMastername();
            if (name == null) {
                name = "";
            }
            txtvUsername.setText(name);

            String age = masterInfo.getAge();
            String ageStr = "未知";
            if (age == null || age.trim().equalsIgnoreCase("")) {
                ageStr = "未知";
            } else {
                ageStr = age + "岁";
            }
            txtvAge.setText(ageStr);

            String serviceNum = masterInfo.getServicenum();
            String serviceNumStr = "未知";
            if(serviceNum == null || serviceNum.trim().equalsIgnoreCase("")) {
                serviceNumStr = "0次";
            } else {
                serviceNumStr = serviceNum + "次";
            }
            txtvServiceNum.setText(serviceNumStr);

            String avatar = masterInfo.getAvatar();
            if (avatar != null && !avatar.trim().equalsIgnoreCase("")) {
                String imageUrl = ImageLoader.getInstance().getImageUrl(avatar);
                ImageLoader.getInstance().displayHeaderPic(MasterDetailActivity.this, imageUrl, imgvAvatar);
            } else {
                ImageLoader.getInstance().displayImageById(MasterDetailActivity.this, R.drawable.ic_master_avatar, imgvAvatar);
            }

            String score = masterInfo.getScore();
            if (score == null || score.trim().equalsIgnoreCase("")) {
                score = "0分";
            } else {
                score += "分";
            }
            txtvScore.setText(score);

            String scoreValue = score.substring(0, score.indexOf("分"));
            ratingBar.setRating(Float.parseFloat(scoreValue));

            String address = masterInfo.getAddress();
            if (address == null || address.trim().equalsIgnoreCase("")) {
                address = "未知";
            }
            txtvAddress.setText(address);
    }
}
